package de.dhbw.karlsruhe.picsim.gui.renderer;

import de.dhbw.karlsruhe.picsim.gui.data.OpCodeGui;

import javax.swing.*;
import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 * User: goesen
 * Date: Apr 15, 2010
 * Time: 8:54:46 AM
 */
public final class CommentRenderer extends ProgramRenderingSupport {
    private final Pattern lineBreak = Pattern.compile("\n");

    @Override
    protected JComponent getRenderingComponent(final OpCodeGui opcode, final JTable jtable, final int row,
                                               final int column) {
        final String newText = "<html><pre>" + opcode.getComments() + "</pre></html>";
        setText(newText);
        final int lines = lineBreak.split(newText).length;

        final int height = lines * jtable.getRowHeight();
        if (jtable.getRowHeight(row) != height) {
            adjustHeight(jtable, row, height);
        }


        return this;
    }

    private static void adjustHeight(final JTable jtable, final int row, final int height) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                if (jtable.getRowHeight(row) != height) {
                    jtable.setRowHeight(row, height);
                }
            }
        });
    }
}
